package com.example.gaming;

import com.example.ajouconqueror.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class conqueringActingMission6 extends Activity implements SensorEventListener{

	
	
	//dong kwan mission
	private int set1 =0;
	private int set2 =0;
	private TextView textarch;

	private int count = 0;
	MediaPlayer backgroundPlayer;
	SoundPool pool;	
	private Vibrator vibrator;	
	SensorManager sensorManager = null;
	final Intent intent = getIntent();
	int track1, track2;
	boolean[] loaded = new boolean[3];
	 
	private static final int REQUEST_QUESTION_MISSION = 101;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		setContentView(R.layout.activity_conquering_acting_mission6);
		// just some textviews, for data output
		
		textarch = (TextView) findViewById(R.id.textturn);
		
		
		backgroundPlayer = MediaPlayer.create(this, R.raw.test_cbr);
		backgroundPlayer.setLooping(true);
		
		pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		pool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
            	Log.d("sound", "Loaded: "+sampleId);
                if(track1 == sampleId) loaded[0] = true;
                else if(track2 == sampleId) loaded[1] = true;
            }
        });
		
		track1 = pool.load(this, R.raw.track1, 0);
		track2 = pool.load(this, R.raw.back, 0);
		 
		String vibratorService = Context.VIBRATOR_SERVICE;
		vibrator = (Vibrator)getSystemService(vibratorService);
	}
	
	@Override
	protected void onPause() {
		pool.autoPause(); 
		backgroundPlayer.pause();
		super.onPause();
	}
	@Override
	protected void onResume() {
		super.onResume();
		pool.autoResume(); 
		backgroundPlayer.start();
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				sensorManager.SENSOR_DELAY_GAME);
	}

	protected void onDestroy() {
		// release sound pool resources
		pool.release();	
		if(backgroundPlayer.isPlaying())
			backgroundPlayer.stop();
		backgroundPlayer.release();		
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		super.onStop();
		sensorManager.unregisterListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
	}
	
	@Override
	public void onBackPressed() {
		doVibration2() ;
		
		if(loaded[1]) {
			pool.play(track2,  1,  1, 0, 0, 1);			
		} 
	}
	

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		  synchronized (this) {
		    	if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){
	        		if((event.values[1]>-90 && event.values[1]<-50) && (event.values[2]>0 && event.values[2]<40)){
	        			set1=1;
	                }
	                else if((event.values[1]>-20 && event.values[1]<20) && (event.values[2]>40 && event.values[2]<100) && set1==1){
	                	set2=1;
	                }
	                else if((event.values[1]>-170 && event.values[1]<-90) && (event.values[2]>-70 && event.values[2]<0) && set2==1){
	                	count += 1;
	                	
	                	
	                	if(loaded[0]) {
							pool.play(track1,  1,  1, 0, 0, 0.5f);			
						} 
						doVibration();
	                	
	                	
	                	textarch.setText(Integer.toString(count));
	                	set1=0;set2=0;
	                }
	                if(count==5){
						Intent qIntent = new Intent(getApplicationContext(), conqueringQuestionMission.class);
//						qIntent.putExtra("building", intent.getStringExtra("buidling"));
						startActivityForResult(qIntent, REQUEST_QUESTION_MISSION);
						finish();
	                }
		    	}
		    }
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if(resultCode == RESULT_OK){
			if(requestCode == REQUEST_QUESTION_MISSION) {
				Toast.makeText(this, "you got point.", Toast.LENGTH_SHORT).show();
				setResult(RESULT_OK, intent);
				finish();
			}
		}
	}
	
	private void doVibration() {
    	
    	// pattern: off, on, off, on,....
    	long[] pattern = {0, 1000};
    	
    	// vibrate using the pattern. Second argument sets repetition from 
    	// index 0. Use -1 if you do not want repetition.
    	vibrator.vibrate(pattern, -1); 

    	//vibrator.vibrate(1000); // vibrate for 1 second (without pattern)
    }
	
  private void doVibration2() {
    	
    	// pattern: off, on, off, on,....
    	long[] pattern = {0, 2000};
    	
    	// vibrate using the pattern. Second argument sets repetition from 
    	// index 0. Use -1 if you do not want repetition.
    	vibrator.vibrate(pattern,-1); 

    	//vibrator.vibrate(1000); // vibrate for 1 second (without pattern)
    }
}
